package com.ruoyi.app.controller.business.audit;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.PageParam;
import com.ruoyi.business.order.domain.JbOrder;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.controller.business.order.factory.JbOrderFactory;
import com.ruoyi.web.controller.business.order.vo.JbOrderVO;
import com.ruoyi.web.core.JoController;
import com.ruoyi.web.core.JoR;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;

/**
 * APP 订单 Controller
 *
 * @author Jyl
 */
@RestController
@RequestMapping("/app/order")
public class AppOrderController extends JoController
{

    private static final Logger log = LoggerFactory.getLogger(AppOrderController.class);

    @Autowired
    private JbOrderFactory jbOrderFactory;

    @ApiOperation("查询订单列表")
    @GetMapping("/list")
    public JSONObject list(PageParam pageParam, JbOrder jbOrder)
    {
        IPage<JbOrderVO> iPage = jbOrderFactory.list(pageParam, jbOrder);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(JoR.rows(iPage)));
        jsonObject.putAll(jbOrderFactory.fnAmount(iPage.getRecords()));
        jsonObject.put("rows", iPage.getRecords());
        return jsonObject;
    }

    @ApiOperation("新增订单草稿")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/draft")
    public AjaxResult draft(@RequestBody JbOrderVO jbOrderVO)
    {
        //下单前 校验金额
        if (jbOrderVO.getCommodityList().isEmpty()) return AjaxResult.error("订单商品数据异常");
        try
        {
            return toAjax(jbOrderFactory.draft(jbOrderVO));
        } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e)
        {
            log.error(e.getMessage());
            return error("保存失败");
        }
    }

    @ApiOperation("新增订单")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JbOrderVO jbOrderVO)
    {
        //下单前 校验金额
        if (jbOrderVO.getCommodityList().isEmpty())
        {
            return AjaxResult.error("订单商品数据异常");
        }
        try
        {
            return toAjax(jbOrderFactory.add(jbOrderVO));
        } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e)
        {
            log.error(e.getMessage());
            return error("新建失败");
        }
    }

    @ApiOperation("修改订单")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JbOrderVO jbOrderVO)
    {
        //下单前 校验金额
        if (jbOrderVO.getCommodityList().isEmpty())
        {
            return AjaxResult.error("订单商品数据异常");
        }
        try
        {
            return toAjax(jbOrderFactory.edit(jbOrderVO));
        } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e)
        {
            log.error(e.getMessage());
            return error("保存失败");
        }
    }

    @ApiOperation("获取订单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jbOrderFactory.getInfo(id));
    }

    @ApiOperation("获取订单详细信息")
    @PreAuthorize("@ss.hasPermi('order:query')")
    @GetMapping(value = "/number/{number}")
    public AjaxResult getInfoByNumber(@PathVariable("number") String number)
    {
        return success(jbOrderFactory.getInfoByNumber(number));
    }

    @ApiOperation("删除订单")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jbOrderFactory.remove(ids));
    }
}
